package com.kjsb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kjsb.dao.ExpertDao;
import com.kjsb.dao.UnitDao;
import com.kjsb.dao.UserDao;
import com.kjsb.entity.Expert;
import com.kjsb.entity.Unit;
import com.kjsb.entity.User;
import com.kjsb.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private ExpertDao expertDao;
    @Autowired
    private UnitDao unitDao;

    @Override
    public User login(String username,String password) {
        User user = userDao.login(username,password);
        return user;
    }

    @Override
    public List<User> getAllUsers() {
        return userDao.selectList(null);
    }

    @Override
    public boolean deleteUsers(Integer id) {
        User user = userDao.selectById(id);
        if(user.getGid()==2){
            Map<String,Object> map =new HashMap<>();
            map.put("uid",user.getUid());
            expertDao.deleteByMap(map);
        }
        if (user.getGid()==3){
            Map<String,Object> map =new HashMap<>();
            map.put("uid",user.getUid());
            unitDao.deleteByMap(map);
        }
        userDao.deleteById(id);
        return true;
    }

    @Override
    public boolean saveUsers(User user) {
        userDao.insert(user);
        if(user.getGid() == 2){
            Expert expert = new Expert();
            expert.setUid(user.getUid());
            expert.setName(user.getUsername());
            expert.setNation("china");
            expertDao.insert(expert);
        }else if(user.getGid() == 3){
            Unit unit = new Unit();
            unit.setUid(user.getUid());
            unit.setUnitname(user.getUsername());
            unitDao.insert(unit);
        }
        return true;
    }

    @Override
    public int updateExpertPassword(User user) {
        return userDao.updateById(user);
    }

    @Override
    public boolean updateUnitPassword(User user) {
        return false;
    }

    @Override
    public User selectByUid(Integer uid) {
        return userDao.selectById(uid);
    }
}
